package code.whmmm.util.collection;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * <p><b>
 *
 * </b></p>
 * <br/>
 *
 * @author Zero
 * @date 2019/8/1 17:24
 */
public class MapUtil {

    private MapUtil() {
    }

    private static final Integer NUM = 2;

    /**
     * <pre>
     *     参数需要严格按照此格式!
     *     k(string),v , k(string),v, k(string),v
     *     参数个数必须是偶数!
     * </pre>
     *
     * @param info k(string),v , k(string),v, k(string),v 数组
     * @return LinkedHashMap&lt;String,Object>
     */
    public static Map<String, Object> createMap(Object... info) {

        if (info == null || info.length == 0) {
            return null;
        }

        if (info.length % NUM != 0) {
            throw new RuntimeException("传递的参数长度 必须是 2 的 整数倍!!");
        }

        Map<String, Object> map = new LinkedHashMap<String, Object>();
        int i = 1;
        for (Object it : info) {
            if (i % 2 == 1 && it == null) {
                continue;
            }
            if (i % 2 == 0) {
                map.put((String) (info[i - 2]), it);
            }
            i++;
        }
        return map;
    }


}
